package com.fand.manager.util;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.fand.common.exception.BusinessException;
import com.fand.manager.entity.SysMenu;
import com.fand.manager.model.menu.IndexMenuDTO;
import com.fand.manager.model.rolemenu.SysRoleMenuTreeDTO;

/**
 * Author: zhangjiabin
 * Email: javaboyok@163.com
 * CreateDate: 2021/6/11
 * Version: 1.0
 */
public class MenuTreeUtil {

    public static List<SysRoleMenuTreeDTO> roleMenu2Tree(List<SysRoleMenuTreeDTO> sysRoleMenuTreeDTOList) {
        List<SysRoleMenuTreeDTO> returnList = CollUtil.newArrayList();
        if (CollUtil.isEmpty(sysRoleMenuTreeDTOList)) {
            throw new BusinessException("非法参数");
        }
        for (SysRoleMenuTreeDTO sysRoleMenuTreeDTO : sysRoleMenuTreeDTOList) {
            if (sysRoleMenuTreeDTO.getPid() == -1L) {
                sysRoleMenuTreeDTO.setChildren(getChildren(sysRoleMenuTreeDTOList, sysRoleMenuTreeDTO.getId()));
                returnList.add(sysRoleMenuTreeDTO);
            }
        }
        return returnList;
    }


    private static List<SysRoleMenuTreeDTO> getChildren(List<SysRoleMenuTreeDTO> sysRoleMenuTreeDTOList, Long pid) {
        List<SysRoleMenuTreeDTO> returnList = CollUtil.newArrayList();
        for (SysRoleMenuTreeDTO sysRoleMenuTreeDTO : sysRoleMenuTreeDTOList) {
            if (sysRoleMenuTreeDTO.getPid().equals(pid)) {
                sysRoleMenuTreeDTO.setChildren(getChildren(sysRoleMenuTreeDTOList, sysRoleMenuTreeDTO.getId()));
                returnList.add(sysRoleMenuTreeDTO);
            }
        }
        return returnList;
    }

    public static List<SysRoleMenuTreeDTO> tree2RoleMenu(List<SysRoleMenuTreeDTO> sysRoleMenuTreeDTOList) {
        List<SysRoleMenuTreeDTO> returnList = CollUtil.newArrayList();
        if (CollUtil.isEmpty(sysRoleMenuTreeDTOList)) {
            return returnList;
        }
        for (SysRoleMenuTreeDTO sysRoleMenuTreeDTO : sysRoleMenuTreeDTOList) {
            returnList.add(sysRoleMenuTreeDTO);
            List<SysRoleMenuTreeDTO> children = sysRoleMenuTreeDTO.getChildren();
            getChildren(returnList, children);
        }
        return returnList;
    }

    private static void getChildren(List<SysRoleMenuTreeDTO> returnList, List<SysRoleMenuTreeDTO> sysRoleMenuTreeDTOList) {
        if (CollUtil.isEmpty(sysRoleMenuTreeDTOList)) {
            return;
        }
        for (SysRoleMenuTreeDTO sysRoleMenuTreeDTO : sysRoleMenuTreeDTOList) {
            returnList.add(sysRoleMenuTreeDTO);
            List<SysRoleMenuTreeDTO> children = sysRoleMenuTreeDTO.getChildren();
            getChildren(returnList, children);
        }
    }

    public static List<IndexMenuDTO.MenuInfoBean> menu2IndexMenu(List<SysMenu> sysMenuList){
        List<IndexMenuDTO.MenuInfoBean> menuInfoList = CollUtil.newArrayList();
        if (CollUtil.isEmpty(sysMenuList)) {
           return menuInfoList;
        }
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getPid() == -1L) {
                IndexMenuDTO.MenuInfoBean menuInfoBean = IndexMenuDTO.MenuInfoBean.builder().title(sysMenu.getName()).target("_self").build();
                menuInfoBean.setChild(getChildrenMenu(sysMenuList,sysMenu.getId()));
                menuInfoList.add(menuInfoBean);
            }
        }
        return menuInfoList;
    }

    private static List<IndexMenuDTO.MenuInfoBean.ChildBean> getChildrenMenu(List<SysMenu> sysMenuList, Long pid) {
        List<IndexMenuDTO.MenuInfoBean.ChildBean> returnList = CollUtil.newArrayList();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getPid().equals(pid)) {
                IndexMenuDTO.MenuInfoBean.ChildBean childBean = IndexMenuDTO.MenuInfoBean.ChildBean.builder().title(sysMenu.getName()).href(sysMenu.getMenuUrl()).target("_self").build();
                childBean.setChild(getChildrenMenu(sysMenuList,sysMenu.getId()));
                returnList.add(childBean);
            }
        }
        return returnList;
    }
}
